package com.example.okhttp;

import android.os.Bundle;
import android.widget.Button;

import androidx.appcompat.app.AppCompatActivity;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class MainActivity extends AppCompatActivity {

    private OkHttpClient client = new OkHttpClient();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button btnSyncGet = findViewById(R.id.btnSyncGet);
        Button btnAsyncGet = findViewById(R.id.btnAsyncGet);
        Button btnSyncPost = findViewById(R.id.btnSyncPost);
        Button btnAsyncPost = findViewById(R.id.btnAsyncPost);

        btnSyncGet.setOnClickListener(v -> {
            // 发送同步GET请求
            new Thread(() -> {
                Request request = new Request.Builder()
                        .url("https://www.httpbin.org/get?key=1&value=2")
                        .build();

                try {
                    Response response = client.newCall(request).execute();
                    String responseData = response.body().string();
                    System.out.println(responseData);
                    // 处理返回的数据
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }).start();
        });

        btnAsyncGet.setOnClickListener(v -> {
            // 发送异步GET请求
            Request request = new Request.Builder()
                    .url("https://www.httpbin.org/get?key=1&value=2")
                    .build();

            client.newCall(request).enqueue(new Callback() {
                @Override
                public void onFailure(Call call, IOException e) {
                    e.printStackTrace();
                }

                @Override
                public void onResponse(Call call, Response response) throws IOException {
                    String responseData = response.body().string();
                    System.out.println(responseData);
                    // 处理返回的数据
                }
            });
        });

        btnSyncPost.setOnClickListener(v -> {
            // 发送同步POST请求
            new Thread(() -> {

                RequestBody requestBody = new FormBody.Builder()
                        // 添加POST请求参数
                        .add("key","1")
                        .add("value","2")
                        .build();

                Request request = new Request.Builder()
                        .url("https://www.httpbin.org/post")
                        .post(requestBody)
                        .build();

                try {
                    Response response = client.newCall(request).execute();
                    String responseData = response.body().string();
                    System.out.println(responseData);
                    // 处理返回的数据
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }).start();
        });

        btnAsyncPost.setOnClickListener(v -> {

            // 构建JSON数据字符串
            String json = "{\"key\": \"1\", \"value\": \"2\"}";

            // 创建RequestBody
            RequestBody requestBody = RequestBody.create(
                    MediaType.parse("application/json"), json);


            Request request = new Request.Builder()
                    .url("https://www.httpbin.org/post")
                    .post(requestBody)
                    .build();

            client.newCall(request).enqueue(new Callback() {
                @Override
                public void onFailure(Call call, IOException e) {
                    e.printStackTrace();
                }

                @Override
                public void onResponse(Call call, Response response) throws IOException {
                    String responseData = response.body().string();
                    System.out.println(responseData);
                }
            });
        });
    }
}